#region

using System;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
using log4net;
using xConnected.Common.Language;
using xConnected.Core.Model;
using xConnected.Service;

#endregion

namespace xConnected.Controllers
{
    public class LanguageController : BaseController<Language>
    {
        private readonly LanguageService _languageService;

        public LanguageController(LanguageService languageService)
        {
            _languageService = languageService;
        }


        public JsonResult List(string search = "", int pageIndex = 0, int pageSize = 10)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var page = search != ""
                               ? _languageService.GetAllPagedSearched(search, pageIndex, pageSize)
                               : _languageService.GetAllPaged(pageIndex, pageSize);
                var languages = page.Entities.ToList().Select(l => new
                                                                       {
                                                                           l.Id,
                                                                           l.Created,
                                                                           l.Updated,
                                                                           Description =
                                                                       LanguageRes.ResourceManager.GetString(l.Key,
                                                                                                             Thread.
                                                                                                                 CurrentThread
                                                                                                                 .
                                                                                                                 CurrentCulture),
                                                                           l.ShortName,
                                                                           l.Key
                                                                       });

                return
                    Json(
                        new {search, page.Count, page.CurrentPage, page.PageSize, page.PagesCount, Entities = languages},
                        JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult Save(Language language)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                _languageService.SaveOrUpdate(language);
                return Json(language.Id, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        [HttpPost]
        public JsonResult DeleteLanguage(int id)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var language = _languageService.GetById(id);
                _languageService.Delete(language);
                return Json("OK", JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult GetLanguagesAutocomplete(string searchTerm = "", int maxRows = 8)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                var languages = _languageService.GetAll().ToList().OrderBy(l =>  LanguageRes.ResourceManager.GetString(l.Key,Thread.CurrentThread.CurrentCulture)).Select(l => new
                {
                    Description = LanguageRes.ResourceManager.GetString(l.Key,Thread.CurrentThread.CurrentCulture),
                    l.Id
                });
                if(searchTerm != "")
                {
                    languages =
                        languages.Where(l => l.Description.ToLower().Contains(searchTerm.ToLower())).Take(maxRows);
                }
                return Json(languages, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
                return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
            }
        }
    }
}